//
//  TT_GroupUserListViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/10/12.
//  Copyright © 2020 王洋. All rights reserved.
//
#import <ImSDK/ImSDK.h>
#import "TTIM_GroupUserCell.h"
#import "SearchResultUserViewController.h"

#import "TT_GroupUserListViewController.h"

@interface TT_GroupUserListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray<TGroupMemberCellData *> *members;
@property V2TIMGroupInfo *groupInfo;

@property (nonatomic, strong) UITableView *tableview;

@end

@implementation TT_GroupUserListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
    [self updateData];
}

- (void)updateData
{
    _members = [NSMutableArray array];
    @weakify(self)
    [[V2TIMManager sharedInstance] getGroupsInfo:@[_groupId] succ:^(NSArray<V2TIMGroupInfoResult *> *groupResultList) {
        @strongify(self)
        if(groupResultList.count == 1){
            self.groupInfo = groupResultList[0].info;
        }
    } fail:^(int code, NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
        [SVProgressHUD dismissWithDelay:1];
    }];
    
    [[V2TIMManager sharedInstance] getGroupMemberList:_groupId filter:V2TIM_GROUP_MEMBER_FILTER_ALL nextSeq:0 succ:^(uint64_t nextSeq, NSArray<V2TIMGroupMemberFullInfo *> *memberList) {
        @strongify(self)
        for (V2TIMGroupMemberFullInfo *member in memberList) {
            TGroupMemberCellData *user = [[TGroupMemberCellData alloc] init];
            user.identifier = member.userID;
            if (member.nameCard.length > 0) {
                user.name = member.nameCard;
            } else if (member.friendRemark.length > 0) {
                user.name = member.friendRemark;
            } else if (member.nickName.length > 0) {
                user.name = member.nickName;
            } else {
                user.name = member.userID;
            }
            [self.members addObject:user];
        }
        
        //刷新tableview
        [self.tableview reloadData];
        
        NSString *title = [NSString stringWithFormat:@"群成员(%ld人)", (long)self.members.count];
        self.title = title;;
    } fail:^(int code, NSString *msg) {

        [SVProgressHUD showErrorWithStatus:msg];
        [SVProgressHUD dismissWithDelay:1];

    }];
}

- (void)setupViews {

    //right
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [rightButton addTarget:self action:@selector(rightBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitle:@"添加成员" forState:UIControlStateNormal];
    [rightButton setTitleColor:UIColorHex(0x333333) forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.parentViewController.navigationItem.rightBarButtonItem = rightItem;
    
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

- (void)rightBarButtonClick {
    
    SearchResultUserViewController *vc = [[SearchResultUserViewController alloc] init];
    
    WEAKSELF
    vc.SearchResultUserBlock = ^(UserInfoModel * _Nonnull model) {
      
        [[V2TIMManager sharedInstance] inviteUserToGroup:weakSelf.groupId userList:@[model.username] succ:^(NSArray<V2TIMGroupMemberOperationResult *> *resultList) {
                [weakSelf updateData];
            
            } fail:^(int code, NSString *desc) {
                    
        }];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    return self.dataArray.count;
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.members.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TTIM_GroupUserCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TTIM_GroupUserCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.members[indexPath.row]];
    
    return cell;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"TTIM_GroupUserCell" bundle:nil] forCellReuseIdentifier:@"TTIM_GroupUserCellID"];
    }
    return _tableview;
}

@end
